package com.mallcai.bigdata.ladon.dpl.mapper.system.provider;

import com.mallcai.bigdata.ladon.dpl.entity.system.User;
import com.mallcai.bigdata.ladon.dpl.mapper.BaseSqlProvider;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author gst
 * @date 2019-12-30 20:36
 */
public class UserSqlProvider extends BaseSqlProvider<User> {
    public static final String TB_USER = "sys_user";

    @Override
    public String getTableName() {
        return TB_USER;
    }

    @Override
    public Map<String, String> getInsertColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("username", "#{username}");
        map.put("password", "#{password}");
        map.put("nickname", "#{nickname}");
        map.put("email", "#{email}");
        map.put("email_active_code", "#{emailActiveCode}");
        map.put("department", "#{department}");
        map.put("position", "#{position}");
        map.put("status", "#{status}");
        map.put("avatar", "#{avatar}");
        map.put("phone", "#{phone}");
        map.put("signature", "#{signature}");
        map.put("address", "#{address}");
        return map;
    }

    @Override
    public Map<String, String> getUpdateColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("username", "username");
        map.put("password", "password");
        map.put("nickname", "nickname");
        map.put("email", "email");
        map.put("emailActiveCode", "email_active_code");
        map.put("department", "department");
        map.put("position", "position");
        map.put("status", "status");
        map.put("avatar", "avatar");
        map.put("phone", "phone");
        map.put("signature", "signature");
        map.put("address", "address");
        return map;
    }


    public String getByParams(Map<String, Object> map) {
        Map<String, Object> params = getParams(map);

        StringBuilder sb = new StringBuilder();
        sb.append("tb_u.archive = 0");
        if (params.containsKey("username") && StringUtils.isNotBlank(params.get("username").toString())) {
            sb.append(" and tb_u.username like concat('%', #{params.username}, '%')");
        }
        if (params.containsKey("nickname") && StringUtils.isNotBlank(params.get("nickname").toString())) {
            sb.append(" and tb_u.nickname like concat('%', #{params.nickname}, '%')");
        }
        if (params.containsKey("email") && StringUtils.isNotBlank(params.get("email").toString())) {
            sb.append(" and tb_u.email like concat('%', #{params.email}, '%')");
        }
        if (params.containsKey("roleId")) {
            sb.append(" and tb_rl.role_id = #{params.roleId}");
        }
        if (params.containsKey("department")) {
            sb.append(" and tb_u.department = #{params.department}");
        }

        StringBuilder fields = new StringBuilder(" DISTINCT tb_u.id AS d_id, tb_u.*, tb_depart.name as departmentName,");
        fields.append("(SELECT group_concat(tb_tmp.name)")
                .append("   FROM (SELECT")
                .append("           tb_r.name,")
                .append("           tb_r.code,")
                .append("           tb_rl.role_id,")
                .append("           tb_rl.user_id,")
                .append("           tb_r.archive")
                .append("         FROM sys_user_rl_role tb_rl LEFT JOIN sys_user_role tb_r ON tb_r.id = tb_rl.role_id) tb_tmp")
                .append("   WHERE tb_tmp.archive = 0 AND tb_tmp.user_id = tb_u.id) AS roleNameStr,");
        fields.append("(SELECT group_concat(tb_tmp.role_id)")
                .append("   FROM (SELECT")
                .append("           tb_r.name,")
                .append("           tb_r.code,")
                .append("           tb_rl.role_id,")
                .append("           tb_rl.user_id,")
                .append("           tb_r.archive")
                .append("         FROM sys_user_rl_role tb_rl LEFT JOIN sys_user_role tb_r ON tb_r.id = tb_rl.role_id) tb_tmp")
                .append("   WHERE tb_tmp.archive = 0 AND tb_tmp.user_id = tb_u.id) AS roleIdStr");
        return new SQL() {
            {
                SELECT(fields.toString());
                FROM(getTableName() + " tb_u ");
                LEFT_OUTER_JOIN(UserRlRoleSqlProvider.TB_USER_RL_ROLE + " tb_rl ON tb_rl.user_id = tb_u.id");
                LEFT_OUTER_JOIN("(select code,name from c_common_info where classify='department' and archive=0) tb_depart on tb_depart.code = tb_u.department");
                WHERE(sb.toString());
            }
        }.toString();
    }

    public String findDetailById(@Param("id") Long id) {
        StringBuilder sb = new StringBuilder();
        sb.append(" tb_u.*,c.name as departmentName,")
                .append(" (")
                .append(" SELECT group_concat(role_id) FROM sys_user_rl_role WHERE user_id = #{id}")
                .append(") AS roleIdStr");
        return new SQL() {
            {
                SELECT(sb.toString());
                FROM(getTableName() + " tb_u ");
                LEFT_OUTER_JOIN(" c_common_info c on c.code = tb_u.department and c.classify='department' and c.archive = 0");
                WHERE(" tb_u.archive = 0 AND tb_u.id = #{id} ");
            }
        }.toString();
    }

    public String isExistEmail(@Param("email") String email, @Param("filterId") Long filterId) {
        StringBuilder sb = new StringBuilder(" archive = 0 ");
        sb.append(" AND email = #{email}");
        if (filterId != null) {
            sb.append(" AND id != #{filterId}");
        }

        return new SQL() {
            {
                SELECT("COUNT(*)");
                FROM(getTableName());
                WHERE(sb.toString());
            }
        }.toString();
    }
}
